#include "Database.h"

Database::Database()
{
	mp_Result = nullptr;
	mp_host = nullptr;
	mp_user = nullptr;
	mp_pwd = nullptr;
	mp_db = nullptr;

	m_isInit = 	false;
	m_isConnect = false;

	m_row = 0;
	m_cols = 0;
	//ctor
}

Database::~Database()
{
	mp_Result = nullptr;
	mp_host = nullptr;
	mp_user = nullptr;
	mp_pwd = nullptr;
	mp_db = nullptr;
	//dtor
}

// 初始化
int Database::Init(string host, string user, string pwd, string db)
{
	mp_host = (char*)host.c_str();
	mp_user = (char*)user.c_str();
	mp_pwd = (char*)pwd.c_str();
	mp_db = (char*)db.c_str();

	// 初始化
	if(NULL == mysql_init(&m_Mysql))
	{
		Debug::Error(__func__, " mysql_init error");
		return -1;
	}

	// 链接
	if(0 != this->Connect())
	{
		return -1;
	}

	m_isInit = true;
	return 0;
}

// 释放
void Database::Dispose()
{
	if(!m_isInit){
		return;
	}

	// 释放结果集
	if(mp_Result != nullptr)
	{
		mysql_free_result(mp_Result);
	}

	// 关闭
	mysql_close(&m_Mysql);
	Debug::State("Close the database:", mp_db, " connect.");
	return;
}

// 链接
int Database::Connect()
{
	if(m_isConnect)
	{
		Debug::Error("The database ", mp_db, "con't connected. Already connected before.");
		return -1;
	}

	if(!mysql_real_connect(&m_Mysql, mp_host, mp_user, mp_pwd, mp_db, 0, nullptr, 0))
	{
		Debug::Error("When connect database, info: ", mysql_error(&m_Mysql));
		return -1;
	}

	Debug::State("Connect database succeed.");
	m_isConnect = true;
	return 0;
}

// 表 查询
int Database::Query(string sql)
{
	if(!m_isInit)
	{
		Debug::Error("Run sql error. please init database.");
		return -1;
	}

	if(-1 == this->RunSql(sql))
	{
		Debug::Error("Database ", mp_host, " Query error.");
		return -1;
	}

	mp_Result = mysql_store_result(&m_Mysql);

	// 打印获取数据行数 和 列数
	m_row = mysql_num_rows(mp_Result);
	m_cols = mysql_num_fields(mp_Result);
	Debug::Log("U got row:", m_row, " cols:", m_cols, " records.");

//	// 查询
//	MYSQL_RES* Res = nullptr;
//	const char* sql2 = "SELECT * FROM tUser;";
//	if(0 != mysql_query(&mysql, sql2)){
//		cout<< "excute sql error. please check your SQL syntax. 1"<< endl;
//		return 0;
//	}
//	Res = mysql_store_result(&mysql);
//	cout<< "query succeed."<< endl;
//
//	// 打印查询结果
//	// 打印获取数据行数
//	int rowCount = mysql_num_rows(Res);
//	cout<< "u got "<< rowCount<< "records."<< endl;
//
//	// 遍历字段
//	int fieldCount = mysql_num_fields(Res);
//	cout<< "cols: "<< fieldCount<< endl;
//	MYSQL_FIELD *Feild = nullptr;
//	for(int i = 0; i < fieldCount; i++){
//		Feild = mysql_fetch_field_direct(Res, i);
//		cout<< Feild->name<< "\t\t";
//	}
//	cout<<endl;
//
//	// 遍历行
//	MYSQL_ROW Row = nullptr;
//	while(NULL != (Row = mysql_fetch_row(Res)))
//	{
//			for(int i = 0; i < rowCount; i++)
//			{
//				cout<< Row[i]<< "\t\t";
//			}
//			cout<< endl;
//	}
//	cout<< "show data over."<< endl;

	return m_row;
}

// 表 添加
int Database::Insert(string sql)
{
	if(!m_isInit)
	{
		Debug::Error("Run sql error. Please init database.");
		return -1;
	}

	if(-1 == this->RunSql(sql))
	{
		Debug::Error("Database ", mp_db, "Insert error.");
		return -1;
	}

	return 0;
}

// 表 删除
int Database::Drop(string sql)
{
	if(!m_isInit)
	{
		Debug::Error("Run sql error. Please init database.");
		return -1;
	}

	if(-1 == this->RunSql(sql))
	{
		Debug::Error("Database ", mp_db, "Drop error.");
		return -1;
	}

	return 0;
}

// 表 更新
int Database::Update(string sql)
{
	if(!m_isInit)
	{
		Debug::Error("Run sql error. Please init database.");
		return -1;
	}

	if(-1 == this->RunSql(sql))
	{
		Debug::Error("Database ", mp_db, "Update error.");
		return -1;
	}
	return 0;
}

// 执行sql语句
int Database::RunSql(string sql)
{
	if(!m_isInit)
	{
		Debug::Error("Run sql error. Please init database.");
		return -1;
	}

	char* sqlStr = (char*)sql.c_str();
	if(0 != mysql_query(&m_Mysql, sqlStr)){
		Debug::Error("Run sql error. Please check your SQL syntax, your sql:\n", sql);
		return -1;
	}

	return 0;
}

